package cn.rocksolid.sport.security.service.test;

import cn.rocksolid.sport.common.Constant;
import cn.rocksolid.sport.common.error.RockSolidException;
import cn.rocksolid.sport.common.utils.Guid;
import cn.rocksolid.sport.security.service.CredentialService;
import cn.rocksolid.sport.test.RockSolidBaseTestCase;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class CredentialServiceTestCase extends RockSolidBaseTestCase {

  private static final String PWD = "test";

  @Autowired
  private CredentialService credentialService;

  @Test
  public void init() {
    credentialService.refresh("test", Constant.AccessType.AGENCY, "123456");
  }

  @Test
  public void verify() {
    String id = Guid.random();
    try {
      credentialService.verify(id, Constant.AccessType.AGENCY, PWD);
    } catch (Exception exp) {
      assertNotNull(exp);
      assertTrue(exp instanceof RockSolidException);
    }
    credentialService.refresh(id, Constant.AccessType.AGENCY, PWD);
    try {
      credentialService.verify(id, Constant.AccessType.MEMBER, PWD);
    } catch (Exception exp) {
      assertNotNull(exp);
      assertTrue(exp instanceof RockSolidException);
    }
    try {
      credentialService.verify(id, Constant.AccessType.BUREAU, PWD);
    } catch (Exception exp) {
      assertNotNull(exp);
      assertTrue(exp instanceof RockSolidException);
    }
    try {
      credentialService.verify(id, Constant.AccessType.BACKOFFICE, PWD);
    } catch (Exception exp) {
      assertNotNull(exp);
      assertTrue(exp instanceof RockSolidException);
    }
    credentialService.verify(id, Constant.AccessType.AGENCY, PWD);
    credentialService.removeById(id);
  }
}
